package com.ecoolex.union.pay.retailer.admin.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.union.pay.retailer.admin.core.AdminConstant;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchant;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantProduct;
import com.ecoolex.union.pay.retailer.pojo.AddPopularMerchantProductDTO;
import com.ecoolex.union.pay.retailer.pojo.EditPopularMerchantProductDTO;
import com.ecoolex.union.pay.retailer.ws.UnionPayPopularMerchantProductService;
import com.ecoolex.union.pay.retailer.ws.UnionPayPopularMerchantService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;


@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "爆款商户商品信息管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = UnionPayPopularMerchantProductGraphql.AUTH, name = "爆款商户商品信息管理")
public class UnionPayPopularMerchantProductGraphql {

    public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-MERCHANT-PRODUCT";

    public static final String AUTH_ADD = AUTH + "-ADD";

    public static final String AUTH_EDIT = AUTH + "-EDIT";

    public static final String AUTH_EDIT_PART = AUTH + "-EDIT-PART";

    public static final String AUTH_GET = AUTH + "-GET";

    public static final String AUTH_QUERY = AUTH + "-QUERY";

    @Reference
    UnionPayPopularMerchantProductService unionPayPopularMerchantProductService;

    @Reference
    UnionPayPopularMerchantService unionPayPopularMerchantService;

    @TypeField(type = UnionPayPopularMerchantProduct.class,field = "merchant",description = "merchant")
    public UnionPayPopularMerchant merchant(@Source UnionPayPopularMerchantProduct source) {
        return unionPayPopularMerchantService.getByMerchantId(source.getMerchantId());
    }

    @QueryField(field = "merchantProductList",description = "爆款商户商品信息")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "爆款商户商品信息")
    public Pagination<UnionPayPopularMerchantProduct> query(@NonNull String merchantId,@NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return unionPayPopularMerchantProductService.queryMerchantProduct(merchantId, page);
    }

    @QueryField(field = "merchantProduct",description = "爆款商户商品信息")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "爆款商户商品")
    public UnionPayPopularMerchantProduct get(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return unionPayPopularMerchantProductService.get(id);
    }

    @MutationField(field = "addMerchantProduct", description = "添加爆款商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加爆款商品")
    public UnionPayPopularMerchantProduct add(@NonNull AddPopularMerchantProductDTO record, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return unionPayPopularMerchantProductService.addMerchantProduct(record, user.userName());
    }

    private static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delMerchantProduct", description = "删除爆款商户商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除爆款商户商品")
    public boolean del(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        UnionPayPopularMerchantProduct product = unionPayPopularMerchantProductService.get(id);
        Assert.isTrue(Check.notNull(product), ResultCode.VERIFY.build(1), "爆款商户商品不存在");
        unionPayPopularMerchantProductService.delete(product, user.userName());
        return true;
    }

    @MutationField(field = "editMerchantProduct", description = "编辑爆款商户商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑爆款商户商品")
    public UnionPayPopularMerchantProduct edit(@NonNull EditPopularMerchantProductDTO record, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        UnionPayPopularMerchantProduct old = unionPayPopularMerchantProductService.get(record.getId());
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商户不存在");
        BeanUtil.copy(record,
        	old,
                Lists.newArrayList(UnionPayPopularMerchantProduct.Column.deleted, UnionPayPopularMerchantProduct.Column.createUser, UnionPayPopularMerchantProduct.Column.createTime).stream().map(
                        UnionPayPopularMerchantProduct.Column::getJavaProperty).toArray(String[]::new));
        return unionPayPopularMerchantProductService.editMerchantProduct(old, user.userName());
    }

}
